package Leetcode.array;

public class Plusone {
    public int[] plusOne(int[] digits) {
        int n = digits.length;
        if (digits[n - 1] != 9)
            digits[n - 1] = digits[n - 1] + 1;

        else {
            int flag = 1;//全是9，要多一位
            int i = n-1;
            for (; i>=0; i--) {
                if (digits[i] != 9) {
                    flag = 0;
                    break;
                }
            }

            if (flag == 1) {
                int[] arr = new int[n + 1];
                arr[0] = 1;
                return arr;
            } else {
                for (int k = i+1; k < n; k++)
                    digits[k] = 0;
                digits[i]++;
                return digits;
            }
        }
        return digits;
    }


    public static void main(String[] args) {
         int[] a={0};
        Plusone plusone=new Plusone();
        int[] b=plusone.plusOne(a);
        for (int i=0;i<b.length;i++)
        System.out.println(b[i]);


    }
}
